package util;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;

import entity.ImageItem;
import entity.User;

public class ItemHelper {
    
    /***
     * Get `number` ImageItem-s from the datastore sorted by rating.
     * Optionally filters by a single tag.  
     * @param offset
     * @param number
     * @param tag, optional
     * @return
     */
    public static List<ImageItem> getLadder(int offset, int number, String tag)
    {
        Query query = new Query("ImageItem");
        //filter by tag?
        if(tag != null)
        {
            query.addFilter(tag + "Tag", Query.FilterOperator.EQUAL, true);
        }
        
        query.addSort("rating", SortDirection.DESCENDING);
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        PreparedQuery pq = ds.prepare(query);
        List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(number).offset(offset));
        List<ImageItem> ret = new ArrayList<ImageItem>();
        for(Entity e: results)
        {
            ret.add(new ImageItem(e));
        }
        //System.out.println("Have " + ret.size());
        return ret;
    }
	
	public static List<ImageItem> getItemsForDuel(int numItems) {
		int numRandomItems = numItems / 2;
		int numNewItems = numItems - numRandomItems;

		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("ImageItem");
		q.addSort("num_duels", SortDirection.ASCENDING);
		PreparedQuery pq = ds.prepare(q);
		List<Entity> duelingList = pq.asList(FetchOptions.Builder.withLimit(numNewItems));
		
		
		q = new Query("__Stat_Kind__");
		q.addFilter("kind_name", FilterOperator.EQUAL, "ImageItem");
		Entity summary = ds.prepare(q).asSingleEntity();
		System.out.println("QQQsummary = " + summary);
		Long totalEntities = 100L;
		if(summary != null){totalEntities = (Long)summary.getProperty("count");}
		
		while (duelingList.size() < numItems) {
			int randomIndex = (int)(Math.random() * totalEntities);
			Entity randomItem = pq.asQueryResultList(FetchOptions.Builder.withLimit(numRandomItems).offset(randomIndex)).get(0);
			if (!duelingList.contains(randomItem)) {
				duelingList.add(randomItem);
			}
		}
			
		List<ImageItem> itemList = new ArrayList<ImageItem>();
	
		for (Entity e : duelingList) {
			itemList.add(new ImageItem(e)); 
		}
	
		return itemList;
	}
    
	public static List<ImageItem> getRandomItems(int numItems) {

		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("ImageItem");
		PreparedQuery pq = ds.prepare(q);
		List<Entity> allEntities = ds.prepare(q).asList(FetchOptions.Builder.withDefaults());
		int totalEntities = allEntities.size();
		// if more items are requested than exist, only return how many exist
		if (totalEntities < numItems) {
			numItems = totalEntities;
		}
		
		List<Entity> randomItemList = new ArrayList<Entity>();
		
		while (randomItemList.size() < numItems) {
			int randomIndex = (int)(Math.random() * totalEntities);
			Entity randomItem = pq.asQueryResultList(FetchOptions.Builder.withLimit(1).offset(randomIndex)).get(0);
			if (!randomItemList.contains(randomItem)) {
				randomItemList.add(randomItem);
			}
		}
			
		List<ImageItem> itemList = new ArrayList<ImageItem>();
	
		for (Entity e : randomItemList) {
			itemList.add(new ImageItem(e)); 
		}
	
		return itemList;
	}
	
	public static List<ImageItem> getRandomItems(int numItems, String tag) {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("ImageItem");
		if(tag != null)
	    {
	        q.addFilter(tag + "Tag", Query.FilterOperator.EQUAL, true);
	    }
		List<Entity> allEntities = ds.prepare(q).asList(FetchOptions.Builder.withDefaults());
		
		int totalEntities = allEntities.size();
		// if more items are requested than exist, only return how many exist
		if (totalEntities < numItems) {
			numItems = totalEntities;
		}
		
		List<Entity> randomItemList = new ArrayList<Entity>();
		
		while (randomItemList.size() < numItems) {
			int randomIndex = (int)(Math.random() * totalEntities);
			if(tag != null)
		    {
		        q.addFilter(tag + "Tag", Query.FilterOperator.EQUAL, true);
		    }
			List<Entity> randomItem = ds.prepare(q).asList(FetchOptions.Builder.withLimit(1).offset(randomIndex));

			if (randomItem.size() != 0 && !randomItemList.contains(randomItem.get(0))) {
				randomItemList.add(randomItem.get(0));
			}
		}
			
		List<ImageItem> itemList = new ArrayList<ImageItem>();
		for (Entity e : randomItemList) {
			itemList.add(new ImageItem(e)); 
		}
	
		return itemList;
	}
	
	public static List<ImageItem> getRandomItems(int numItems, String tag, ImageItem excludeItem) {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("ImageItem");
		if(tag != null)
	    {
	        q.addFilter(tag + "Tag", Query.FilterOperator.EQUAL, true);
	    }
		List<Entity> allEntities = ds.prepare(q).asList(FetchOptions.Builder.withDefaults());
		
		int totalEntities = allEntities.size();
		// if more items are requested than exist, only return how many exist
		if (totalEntities <= numItems) {
			numItems = totalEntities-1;
		}
		
		List<Entity> randomItemList = new ArrayList<Entity>();
		
		while (randomItemList.size() < numItems) {
			int randomIndex = (int)(Math.random() * totalEntities);
			if(tag != null)
		    {
		        q.addFilter(tag + "Tag", Query.FilterOperator.EQUAL, true);
		    }
			List<Entity> randomItem = ds.prepare(q).asList(FetchOptions.Builder.withLimit(1).offset(randomIndex));
			if (randomItem.size() != 0 && !randomItemList.contains(randomItem.get(0))) {
				if (excludeItem == null) {
					randomItemList.add(randomItem.get(0));
				} else if (!(excludeItem.getUrl().equals(randomItem.get(0).getKey().getName())) ) {
					randomItemList.add(randomItem.get(0));
				}
			}

		}
		
		List<ImageItem> itemList = new ArrayList<ImageItem>();
		for (Entity e : randomItemList) {
			itemList.add(new ImageItem(e)); 
		}
		
		return itemList;
	}
	/*
     * Helper method that calculates ELO adjustment, using a slightly modified version of the FIDE formula
     * @param r1, rating of player1
     * @param r2, rating of player2
     * @param winner, true is player1 won; false if player2 won
     */
    public static int calcEloAdjustment(int r1, int r2, boolean winner) {
        int max_adj = 32;
        double multiplier = 1.0;
        if (winner)
            multiplier = (1/(1+Math.pow(10.0,(r1-r2)/400.0)));
        else
            multiplier = (1/(1+Math.pow(10.0,(r2-r1)/400.0)));
        int adjustment = (int)Math.round(max_adj * multiplier);
        return adjustment;
    }
    
    /* adjust the stats of two items given a winner */
    public static void adjustStats(ImageItem i1, ImageItem i2, User u1, User u2, boolean winner) {
    	int r1 = i1.getRating();
    	int r2 = i2.getRating();
    	int adjustment = calcEloAdjustment(r1, r2, winner);
    	if (winner) {
    		i1.setRating(r1+adjustment);
    		i2.setRating(r2-adjustment);
    		u1.modScore(adjustment);
    		u2.modScore(adjustment*-1);
    	} else {
    		i1.setRating(r1-adjustment);
    		i2.setRating(r2+adjustment);
    		u1.modScore(adjustment*-1);
    		u2.modScore(adjustment);
    	}
    	
    	i1.setNum_duels(i1.getNum_duels()+1);
    	i2.setNum_duels(i2.getNum_duels()+1);
    	
		u1.modCount(1);
		u2.modCount(1);
    }
    
    /* given the resulting ordering of a battle, commit changes to respective ratings */
    public static void resolveDuel(List<String> itemList) {
		ImageItem [] items = new ImageItem[4];
		User [] users = new User[4];
		for (int i=0; i < 4; i++) {
			items[i] = ImageItem.get(itemList.get(i));
			users[i] = User.get(items[i].getUser());
		}
    	
    	adjustStats(items[0], items[1], users[0], users[1], true);
    	adjustStats(items[0], items[2], users[0], users[2], true);
    	adjustStats(items[0], items[3], users[0], users[3], true);
    	adjustStats(items[1], items[2], users[1], users[2], true);
    	adjustStats(items[1], items[3], users[1], users[3], true);
    	adjustStats(items[2], items[3], users[2], users[3], true);
    	
    	for (int i=0; i < 4; i++) {
			items[i].save();
			users[i].save();
		}
    }
    
	public static ImageItem[] loadStatic(int batchNum) {
		ArrayList<String[]> itemData = new ArrayList<String[]>();
		itemData.add(new String[] {"http://i.imgur.com/0kc0s.jpg", "Baby leopards?", "", "cat|pets", "1729", "18", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/7p0ko.jpg", " Stray porch kitty", "", "cat|pets", "1568", "11", "KindlyKitty"});
		itemData.add(new String[] {"http://i.imgur.com/WgV0q.jpg", " Struttin'", "", "cat", "1366", "5", "CrazyCatLady"});
		itemData.add(new String[] {"http://i.imgur.com/uNNkL.jpg", " Ebay Stray Kitten", "", "cat", "1483", "8", "AdorableAl"});
		itemData.add(new String[] {"http://i.imgur.com/IHQlz.jpg", " Captain Cat", "", "cat", "1718", "9", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/Eiixf.jpg", " Downcast Fuzzball", "", "dog", "1253", "18", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/PDxH5.jpg", " Drink Up", "", "cat|pets", "1435", "11", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/IraC6.jpg", " Blissful comfort", "", "cat", "1336", "13", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/sQgYx.jpg", " I sleep how I wants", "", "cat", "1409", "18", "TheCuteConnoisseur"});
		itemData.add(new String[] {"http://i.imgur.com/Gw5e9.jpg", " Jazz Hand Cat", "", "cat", "1516", "17", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/Y1mkX.jpg", " Bathtime sadness", "", "cat", "1644", "2", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/kSkux.jpg", " She fought the shoe and the shoe won", "", "cat", "1434", "19", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/xSYVy.jpg", " aTitle16", "", "", "1504", "20", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/bYnZA.jpg", " Family Cat Photography", "", "cat|pets", "1755", "20", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/kKNAN.jpg", " Here's looking at you", "", "cat|pets", "1702", "18", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/sjxWT.jpg", " I'm a pine cone", "", "wildlife", "1442", "8", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/pVMWE.jpg", " I've fallen and I can't get up!", "", "wildlife", "1321", "4", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/U3Txt.jpg", " What's up doc?", "", "wildlife", "1474", "3", "dtm988"});
		itemData.add(new String[] {"http://i.imgur.com/hz90a.jpg", " Tomatoes give you peace", "", "cat|pets", "1581", "3", "KindlyKitty"});
		itemData.add(new String[] {"http://i.imgur.com/x6tXk.jpg", " To the moon!", "", "wildlife", "1370", "9", "PuppyOverload"});
		itemData.add(new String[] {"http://i.imgur.com/EkPir.jpg", " Why does this egg feel so large?", "", "dog|bird", "1357", "20", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/Umz3e.jpg", " Feeding the Managerie", "", "pets", "1493", "5", "KindlyKitty"});
		itemData.add(new String[] {"http://i.imgur.com/YTIzZ.jpg", " My teddy pig...", "", "wildlife", "1319", "2", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/bitj7.jpg", " Best buds", "", "pets|bird", "1309", "8", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/051pE.jpg", " Aww...", "", "cat|pets", "1233", "16", "KindlyKitty"});
		itemData.add(new String[] {"http://i.imgur.com/09C5G.jpg", " Battle helmets...on!", "", "wildlife", "1238", "1", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/0sAOA.jpg", " Bwahahaha!!!", "", "", "1509", "20", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/1j5WR.jpg", " Yin-yang", "", "cat|pets", "1690", "3", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/lyEMJ.jpg", " What'ya looking at?", "", "cat|pets", "1372", "16", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/2SUON.jpg", " Yo! What's up?", "", "dog|pets", "1541", "12", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/2tGUi.jpg", " Please get me out of this dirty place!", "", "cat|pets", "1415", "5", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/2zGI0.jpg", " The bleached managerie and its newest member!", "", "cat|dog|pets", "1510", "16", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/3rrs5.jpg", " We're going to need more cats...", "", "cat|pets", "1288", "20", "AdorableAl"});
		itemData.add(new String[] {"http://i.imgur.com/3SCdT.jpg", " My kitty to hold", "", "human|cat|pets", "1723", "6", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/7G2lS.jpg", " Bear return", "", "pets", "1254", "7", "CrazyCatLady"});
		itemData.add(new String[] {"http://i.imgur.com/7NHEL.png", " He doesn't know...", "", "cat|wildlife", "1485", "3", "CrazyCatLady"});
		itemData.add(new String[] {"http://i.imgur.com/9nhJ8.jpg", " Let me at 'em!", "", "cat|dog|pets", "1305", "2", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/9Uv0R.jpg", " What is this all about?", "", "dog|pets", "1608", "4", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/a5mr6.jpg", " Keepsake turtle", "", "pets", "1521", "12", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/aLCZW.jpg", " Shh...", "", "cat|pets", "1418", "9", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/aLkUc.jpg", " I'm Super Bird!", "", "wildlife|bird", "1372", "17", "TheCuteConnoisseur"});
		itemData.add(new String[] {"http://i.imgur.com/AMSWF.jpg", " All heil the kitty!", "", "cat|pets", "1732", "12", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/ANCb9.jpg", " Pileup on the kitty freeway", "", "cat|pets", "1278", "10", "Awwdorable"});
		itemData.add(new String[] {"http://i.imgur.com/bd8JN.jpg", " Here's your cat", "", "wildlife|cat", "1553", "4", "Awwdorable"});
		itemData.add(new String[] {"http://i.imgur.com/BpBkl.jpg", " Tail-tagging along", "", "cat|dog|pets", "1446", "19", "Awwdorable"});
		itemData.add(new String[] {"http://i.imgur.com/ciqY0.jpg", " The Express Bus", "", "wildlife", "1699", "3", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/czAFG.jpg", " Kitty kitty", "", "cat", "1739", "14", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/D3o72.jpg", " Model cat", "", "cat|pets", "1649", "1", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/D5pW9.png", " Here's how it's going to go down...", "", "dog|pets", "1658", "17", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/eKJnv.jpg", " Top hat cat", "", "cat|pets", "1330", "12", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/eSDDU.jpg", " Attack of the killer bubble cat!", "", "cat|human", "1413", "1", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/F43zN.jpg", " Teddy bear for all ages", "", "cat|pets", "1703", "3", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/fcfF7.jpg", " Bunny Yoga", "", "pets", "1743", "3", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/fI2gl.jpg", " Yeah--- leaf!", "", "wildlife", "1759", "2", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/fxvrU.jpg", " This is a flower-- right?", "", "wildlife", "1372", "3", "KindlyKitty"});
		itemData.add(new String[] {"http://i.imgur.com/GbvT7.jpg", " High one!", "", "cat|human|pets", "1471", "13", "KindlyKitty"});
		itemData.add(new String[] {"http://i.imgur.com/GLiEo.jpg", " We shouldn't have eaten so much....", "", "wildlife", "1622", "14", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/GzrV8.jpg", " The AT&T Free Cat Offer!", "", "cat|pets", "1620", "6", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/rBdZV.jpg", " Honey-- I blew up the cat!", "", "cat|pets", "1264", "12", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/HAgja.jpg", " The Cat Rug", "", "cat|pets", "1587", "14", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/IaKxd.jpg", " Tasting the meal", "", "cat|dog|pets", "1672", "13", "PuppyOverload"});
		itemData.add(new String[] {"http://i.imgur.com/jVpOB.jpg", " I'm fat-- but I'm cold", "", "cat|dog", "1226", "13", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/jzGIv.jpg", " Next Shrek film", "", "", "", "", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/Kh6Vs.jpg", " Missing doll-- what missing doll?", "", "cat|pets", "1307", "10", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/KIz7s.jpg", " Get away from my kid!", "", "cat|pets", "1275", "1", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/kOfiH.jpg", " Ducks on the duck boat", "", "wildlife", "1300", "19", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/LJe2J.jpg", " Another one of Bambi's friends", "", "wildlife|cats", "1358", "15", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/lZJ8p.jpg", " Oh-- why did I look down?", "", "cat|pets", "1511", "17", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/lZUw9.jpg", " Mr. Whiskers", "", "cat|pets", "1404", "10", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/mOC0k.jpg", " Cat on Deck", "", "cat", "1502", "5", "AdorableAl"});
		itemData.add(new String[] {"http://i.imgur.com/MTm67.jpg", " Chillaxing...", "", "cat|pets", "1727", "20", "AdorableAl"});
		itemData.add(new String[] {"http://i.imgur.com/N6dS3.jpg", " Dude--  you're supposed to catch me!", "", "wildlife", "1628", "1", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/n7Xfn.jpg", " Cuddling Cutie Cats", "", "cat|pets", "1634", "1", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/nNuuW.jpg", " Siamese Triplet Cats", "", "cat|pets", "1349", "18", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/NUDtZ.jpg", " Nap Time", "", "cat|pets", "1791", "2", "ravishrestha01"});		
		itemData.add(new String[] {"http://i.imgur.com/oAj7Y.jpg", " I'm the next elmo", "", "cat|pets", "1283", "9", "PuppyOverload"});
		itemData.add(new String[] {"http://i.imgur.com/ODvWU.jpg", " I'm trapped!", "", "human|cat", "1342", "10", "PuppyOverload"});
		itemData.add(new String[] {"http://i.imgur.com/pTjuC.jpg", " Really Flat Cat", "", "cat|pets", "1753", "13", "PuppyOverload"});
		itemData.add(new String[] {"http://i.imgur.com/pUBTP.png", " Felled Over", "", "pets", "1628", "13", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/PPHnY.jpg", " Nighty Night", "", "cat|pets", "1301", "10", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/qcbwG.jpg", " March of the Pandas", "", "wildlife", "1638", "8", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/r1SZ1.jpg", " Cuddle with me", "", "cat|pets", "1275", "14", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/R27b8.jpg", " Tongue", "", "cat|pets", "1223", "13", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/rTDty.jpg", " A bowl of kitty", "", "cat|pets", "1488", "11", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/SjztC.jpg", " A dog", "", "dog", "1678", "8", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/Suqee.jpg", " Basket Case Cats", "", "cat|pets", "1509", "9", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/taoBK.jpg", " Cat Supplication", "", "cat|pets", "1356", "20", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/tDhCW.jpg", " I dont' care if it's just a head-- it's my doll!", "", "cat|pets", "1251", "16", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/TeX1P.jpg", " Bubble Bathtime", "", "pets", "1461", "1", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/TNf3B.jpg", " So relaxed", "", "cat|pets", "1486", "17", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/to2j7.jpg", " The cat ladder", "", "cat|pets", "1545", "17", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/UG2lU.jpg", " I love you mom", "", "cat|pets", "1476", "3", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/uklWq.jpg", " Sunlight! Finally!", "", "wildlife", "1785", "14", "Awwdorable"});
		itemData.add(new String[] {"http://i.imgur.com/Uq5uB.jpg", " Cathernet", "", "cat|pets", "1683", "13", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/wkRT0.jpg", " Oh Yeah!", "", "cat|human|pets", "1567", "15", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/V7XM3.jpg", " There's always the odd one out", "", "cat|pets", "1663", "6", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/v8bvW.jpg", " Duck gym class", "", "wildlife", "1674", "12", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/VFHC6.jpg", " nom nom nom", "", "wildlife", "1638", "19", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/VtkMC.jpg", " A Dog and his kitty", "", "dog|cat|pets", "1664", "18", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/Wdh6P.jpg", " Chocolate-claw cat", "", "cat|pets", "1645", "14", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/we1EG.jpg", " Get him off of me!", "", "cat|pets", "1313", "2", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/xOacj.jpg", " Corn", "", "", "", "", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/XYb5J.jpg", " Hot Dog Wrap", "", "dog|pets", "1321", "16", "AdorableAl"});
		itemData.add(new String[] {"http://i.imgur.com/xznY5.jpg", " Bah!", "", "cat", "1654", "17", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/Y0PCI.jpg", " Easter Bonnet Cat", "", "cat", "1572", "13", "TheCuteConnoisseur"});
		itemData.add(new String[] {"http://i.imgur.com/ODCxu.jpg", " The Wolf Inside", "", "dog|wildlife", "1314", "2", "TheCuteConnoisseur"});
		itemData.add(new String[] {"http://i.imgur.com/y2tBm.jpg", " The Cat Cave", "", "cat|pets", "1796", "11", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/zduRm.jpg", " 1st Rule of the Buddy System", "", "cat|pets", "1505", "15", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/LkwBy.jpg", " Jekyll and Hyde", "", "cat|pets", "1633", "3", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/F6BrU.jpg", " Bosely Medical Center for Ear Restoration", "", "dog|pets", "1251", "20", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/ALodn.jpg", " Yawn....", "", "cat", "1335", "6", "ravishrestha01"});
		itemData.add(new String[] {"http://i.imgur.com/gNIB6.jpg", " Yum yum", "", "cat|pets", "1745", "19", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/uo69N.jpg", " Coming soon to a tree near you...", "", "wildlife", "1253", "7", "aCuriousTA"});
		itemData.add(new String[] {"http://i.imgur.com/3j3rU.jpg", " I've heard of computer bugs before-- but this?", "", "cat|pets", "1495", "1", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/MpqfR.jpg", " Cat as a Hat", "", "cat|pets|human", "1317", "18", "PuppyOverload"});
		itemData.add(new String[] {"http://i.imgur.com/wAs6h.jpg", " Cat on Dog", "", "cat|dog|pets", "1462", "5", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/uYaqG.jpg", " Can someone color me in?", "", "cat|pets", "1436", "20", "AdorableAl"});
		itemData.add(new String[] {"http://i.imgur.com/bcv2X.jpg", " The Dog as Zerg", "", "dog|pets", "1290", "7", "AdorableAl"});
		itemData.add(new String[] {"http://i.imgur.com/0TNbK.jpg", " The Sleeping Monks", "", "pets", "1584", "6", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/rV1vk.jpg", " A flower for you-- pal!", "", "dog|pets", "1663", "18", "KindlyKitty"});
		itemData.add(new String[] {"http://i.imgur.com/OBEL8.jpg", " I bite my fingernails when I sleep", "", "cat|pets", "1502", "7", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/xa7nU.jpg", " Pick of the Litter", "", "dog|pets", "1464", "6", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/6VV9m.jpg", " The new Kermit the Dog", "", "dog|pets", "1326", "20", "PuppyOverload"});
		itemData.add(new String[] {"http://i.imgur.com/WPEJd.jpg", " Come inside and think about what you've done...", "", "dog|pets", "1646", "9", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/x91IE.jpg", " Ice Weasel", "", "wildlife", "1315", "2", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/myDMM.jpg", " The A Team", "", "pets|cat", "1563", "5", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/sa6LM.jpg", " I was jumping...", "", "dog|pets", "1597", "4", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/BgrPp.png", " Synchronized Napping", "", "cat|pets", "1739", "14", "TheCuteConnoisseur"});
		itemData.add(new String[] {"http://i.imgur.com/MJXpQ.jpg", " I'm bored...", "", "cat", "1568", "11", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/talor.jpg", " Boyfriend and Girlfriend", "", "cat|dog|pets", "1395", "14", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/NPLxt.jpg", " Boo!", "", "cat", "1698", "4", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/TNrTd.jpg", " Being Close to Your Food", "", "dog|pets", "1354", "1", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/PMnOv.jpg", " Kleenex Cat", "", "cat|pets", "1605", "4", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/Og2lw.jpg", " The Leopard Cat", "", "cat", "1377", "11", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/Mpda0.jpg", " Staring Contest", "", "cat|pets", "1375", "1", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/eHSL2.jpg", " Doggy Swing Set", "", "dog|pets", "1359", "10", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/0gdOi.jpg", " Here's the sun-- little kitty", "", "cat|pets", "1214", "9", "TheCuteConnoisseur"});
		itemData.add(new String[] {"http://i.imgur.com/KBhbT.jpg", " My doll is in there", "", "cat|pets", "1548", "15", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/R85qs.jpg", " Elephant Cast", "", "human|wildlife", "1268", "15", "Awwdorable"});
		itemData.add(new String[] {"http://i.imgur.com/zWCi8.jpg", " Whoa", "", "cat", "1742", "8", "inaset33"});
		itemData.add(new String[] {"http://i.imgur.com/HVSbh.jpg", " Kitty Wristwatch", "", "cat|human|pets", "1729", "8", "inaset33"});	
		itemData.add(new String[] {"http://i.imgur.com/ggm0V.jpg", "Hungry Birds!", "", "bird", "1721", "8", "BirdWatcher"});
		itemData.add(new String[] {"http://i.imgur.com/Bz8WP.jpg", "Waddling Penguin", "", "bird", "1618", "4", "BirdWatcher"});
		itemData.add(new String[] {"http://i.imgur.com/hiXoI.jpg", "Pyramid of Birds", "", "bird", "1320", "9", "BirdWatcher"});
		itemData.add(new String[] {"http://i.imgur.com/AjsBS.jpg", "Fed at her expense", "", "bird", "1556", "3", "BirdWatcher"});
		itemData.add(new String[] {"http://i.imgur.com/UVXZN.jpg", "Odd one out", "", "bird", "1801", "12", "BirdWatcher"});
		itemData.add(new String[] {"http://i.imgur.com/TH5f1.jpg", "Just a baby bird", "", "bird", "1301", "6", "BirdWatcher"});
		itemData.add(new String[] {"http://i.imgur.com/7Tis3.jpg", "Squirrels spooning", "", "wildlife", "1221", "9", "NatureLover"});
		itemData.add(new String[] {"http://i.imgur.com/B88tp.jpg", "Puppy Love", "", "human|pets", "1123", "8", "BabyLoader"});
		itemData.add(new String[] {"http://i.imgur.com/bLfiy.jpg", "Confusing baby", "", "human|dog|pets", "1421", "11", "BabyLoader"});
		itemData.add(new String[] {"http://i.imgur.com/uGG8W.jpg", "NapTime", "", "human|cat|pets", "1500", "14", "BabyLoader"});
		itemData.add(new String[] {"http://i.imgur.com/uEGEQ.jpg", "Nuzzling Rabit", "", "human|wildlife", "1345", "17", "BabyLoader"});
		itemData.add(new String[] {"http://i.imgur.com/evkMw.jpg", "Love and Loyalty", "", "human|dog|pets", "1291", "3", "BabyLoader"});
		
		ImageItem[] itemArray = new ImageItem[itemData.size()];
		
		if (batchNum == 1) {
			for (int kk = 0; kk < 40; kk++) {
				String[] item = itemData.get(kk);
				try {
					itemArray[kk] = new ImageItem(
							item[0],
							item[1],
							item[2],
							item[3].contains("cat") ? true : false,
							item[3].contains("dog") ? true : false,
							item[3].contains("bird") ? true : false,
							item[3].contains("pets") ? true : false,
							item[3].contains("wildlife") ? true : false,
							item[3].contains("human") ? true : false,					
							item[4] == "" ? 1500 : Integer.parseInt(item[4]),
							item[5] == "" ? 1 : Integer.parseInt(item[5]),
							item[6]
							);	
				} catch(Exception e){
					System.out.println("warning: error in index " + kk);
				}			
			}
		} else if (batchNum == 2) {
			for (int kk = 40; kk < 80; kk++) {
				String[] item = itemData.get(kk);
				try {
					itemArray[kk] = new ImageItem(
							item[0],
							item[1],
							item[2],
							item[3].contains("cat") ? true : false,
							item[3].contains("dog") ? true : false,
							item[3].contains("bird") ? true : false,
							item[3].contains("pets") ? true : false,
							item[3].contains("wildlife") ? true : false,
							item[3].contains("human") ? true : false,					
							item[4] == "" ? 1500 : Integer.parseInt(item[4]),
							item[5] == "" ? 1 : Integer.parseInt(item[5]),
							item[6]
							);	
				} catch(Exception e){
					System.out.println("warning: error in index " + kk);
				}			
			}
		} else if (batchNum == 3) {
			for (int kk = 80; kk < 120; kk++) {
				String[] item = itemData.get(kk);
				try {
					itemArray[kk] = new ImageItem(
							item[0],
							item[1],
							item[2],
							item[3].contains("cat") ? true : false,
							item[3].contains("dog") ? true : false,
							item[3].contains("bird") ? true : false,
							item[3].contains("pets") ? true : false,
							item[3].contains("wildlife") ? true : false,
							item[3].contains("human") ? true : false,					
							item[4] == "" ? 1500 : Integer.parseInt(item[4]),
							item[5] == "" ? 1 : Integer.parseInt(item[5]),
							item[6]
							);	
				} catch(Exception e){
					System.out.println("warning: error in index " + kk);
				}			
			}
		} else if (batchNum == 4) {
			for (int kk = 120; kk < 152; kk++) {
				String[] item = itemData.get(kk);
				try {
					itemArray[kk] = new ImageItem(
							item[0],
							item[1],
							item[2],
							item[3].contains("cat") ? true : false,
							item[3].contains("dog") ? true : false,
							item[3].contains("bird") ? true : false,
							item[3].contains("pets") ? true : false,
							item[3].contains("wildlife") ? true : false,
							item[3].contains("human") ? true : false,					
							item[4] == "" ? 1500 : Integer.parseInt(item[4]),
							item[5] == "" ? 1 : Integer.parseInt(item[5]),
							item[6]
							);	
				} catch(Exception e){
					System.out.println("warning: error in index " + kk);
				}			
			}
		}
			
		return itemArray;
	}
}
